
import {
  getEventListApi,
  getEventDetailByTailNumberApi,
  addCommentApi,
  processingExportExcelApi,
  uploadApi,
  downloadApi,
  removeFileApi,
  getEventBySlopeApi,
  getListAirportAllApi
} from '@/services/eventMonitoring.js';
import { message } from 'ant-design-vue';
export default {
  namespaced: true,
  state: {
    paginationOptions: {
      pageNum: 1,
      pageSize: 50,
      total: 0
    },
    eventListLoading: false,
    loading: false,
    eventList: [],
    // 事件详情
    eventDetailLoading: false,
    eventDetail: [],
    addCommentLoading: false,
    processingExportExcelLoading: false,
    uploadLoading: false,
    downloadLoading: false,
    eventBySlope: [],
    eventBySlopeLoading: false,
    listAirportLoading: false,
    listAirportData: [],
    // 1 事件监控/历史 2 消息 3 航段详情
    dataType: -1,
    dataTypeIndex: [-1]
  },
  mutations: {
    setState(state, { key, payload }) {
      state[key] = payload;
    }
  },
  actions: {
    // 获取事件监控
    async getEventList({ commit, state, dispatch, rootState }, params = {}) {
      // debugger;
      if (!params.noLoading) {
        commit('setState', { key: 'eventListLoading', payload: true });
      }

      const { pageNum, pageSize } = state.paginationOptions;
      const apiParams = {
        pageNum,
        pageSize,
        ...params
      };
      params.alertCode && params.alertCode[0] === 'All' ? apiParams.alertCode = rootState.fraMonitoring.alertCodeListList.flatMap(item => item.code) : '';
      params.mmsgCode && params.mmsgCode[0] === 'All' ? apiParams.mmsgCode = rootState.fraMonitoring.mmsgCodeListList.flatMap(item => item.code) : '';
      params.fdeCode && params.fdeCode[0] === 'All' ? apiParams.fdeCode = rootState.fraMonitoring.fdeCodeListList.flatMap(item => item.code) : '';

      const rep = await getEventListApi(apiParams);
      dispatch('setting/setAcarsTime', {}, { root: true });
      commit('setState', { key: 'paginationOptions', payload: { ...state.paginationOptions, total: rep.data.total }});
      commit('setState', { key: 'eventList', payload: rep.data.list });
      if (!params.noLoading) {
        commit('setState', { key: 'eventListLoading', payload: false });
      }
    },
    // 获取事件详情
    async getEventDetailByTailNumber({ commit, state }, params) {
      commit('setState', { key: 'eventDetailLoading', payload: true });
      const rep = await getEventDetailByTailNumberApi(params);
      commit('setState', { key: 'eventDetail', payload: rep.data });
      commit('setState', { key: 'eventDetailLoading', payload: false });
    },
    // 获取全量机场
    async getListAirportAll({ commit, state }, params) {
      commit('setState', { key: 'listAirportLoading', payload: true });
      const rep = await getListAirportAllApi(params);

      // function duplicate(arr) {
      //   var result = [];
      //   const arrf = arr.flatMap(item1 => item1.threeCode);
      //   arrf.forEach(function(item) {
      //     if (arrf.indexOf(item) !== arrf.lastIndexOf(item) && result.indexOf(item) === -1) {
      //       result.push(item);
      //     }
      //   });
      //   return result;
      // }
      // const a = duplicate(rep.data);
      // console.log(a);

      const newData1 = (rep.data ?? []).sort((a, b) => (a?.threeCode ?? 0).localeCompare((b?.threeCode ?? 0)));
      commit('setState', { key: 'listAirportData', payload: newData1 });
      commit('setState', { key: 'listAirportLoading', payload: false });
    },
    // 获取斜率
    async getEventBySlope({ commit, state }, params) {
      commit('setState', { key: 'eventBySlopeLoading', payload: true });
      const rep = await getEventBySlopeApi(params);
      commit('setState', { key: 'eventBySlope', payload: rep.data });
      commit('setState', { key: 'eventBySlopeLoading', payload: false });
    },
    // 新增评论
    async addComment({ commit }, params) {
      commit('setState', { key: 'addCommentLoading', payload: true });
      try {
        const rep = await addCommentApi(params);
        if (rep.code === 200) {
          message.success(rep.message);
          return Promise.resolve(true);
        } else {
          message.error(rep.message);
        }
      } catch (error) {
        message.error('操作失败');
      } finally {
        commit('setState', { key: 'addCommentLoading', payload: false });
      }
    },
    // 导出
    async processingExportExcel({ commit, state, dispatch, rootState }, params) {
      commit('setState', { key: 'processingExportExcelLoading', payload: true });
      try {
        const apiParams = {
          ...params
        };
        params.alertCode && params.alertCode[0] === 'All' ? apiParams.alertCode = rootState.fraMonitoring.alertCodeListList.flatMap(item => item.code) : '';
        params.mmsgCode && params.mmsgCode[0] === 'All' ? apiParams.mmsgCode = rootState.fraMonitoring.mmsgCodeListList.flatMap(item => item.code) : '';
        params.fdeCode && params.fdeCode[0] === 'All' ? apiParams.fdeCode = rootState.fraMonitoring.fdeCodeListList.flatMap(item => item.code) : '';

        const rep = await processingExportExcelApi(apiParams);
        return Promise.resolve(rep);
      } catch (error) {
        message.error('操作失败');
      } finally {
        commit('setState', { key: 'processingExportExcelLoading', payload: false });
      }
    },

    async removeFile({ commit }, params) {
      commit('setState', { key: 'removeFileLoading', payload: true });
      try {
        const rep = await removeFileApi(params);
        if (rep.code === 200) {
          message.success('操作成功');
          return Promise.resolve(true);
        }
      } catch (error) {
        message.error('操作失败');
      } finally {
        commit('setState', { key: 'removeFileLoading', payload: false });
      }
    },
    // 上传
    async uploadFile({ commit }, params) {
      commit('setState', { key: 'uploadLoading', payload: true });
      try {
        const rep = await uploadApi(params);
        if (rep.code === 200) {
          message.success('操作成功');
          return Promise.resolve(rep);
        }
        // } else {
        //   message.error(rep.message);
        // }
      } catch (error) {
        message.error('操作失败');
      } finally {
        commit('setState', { key: 'uploadLoading', payload: false });
      }
    },
    async downLoadFile({ commit }, params) {
      commit('setState', { key: 'downloadLoading', payload: true });
      try {
        const rep = await downloadApi(params);
        return Promise.resolve(rep);
      } catch (error) {
        message.error('操作失败');
      } finally {
        commit('setState', { key: 'downloadLoading', payload: false });
      }
    }

  }
};
